#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：18.session会话.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-20 14:54
'''
import requests

"""
session的本质其实是和网站保持一个长连接
"""

headers = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36 Edg/131.0.0.0"
}

session = requests.Session()

url_1 = "https://www.baidu.com"

response = session.get(url_1, headers=headers)
print("第一次请求的请求头数据为：", response.request.headers)
print("第一次请求的响应头数据为：", response.headers)


response.request.headers['cookie'] = dict(response.cookies)
print(response.request.headers)



url_2 = "https://www.baidu.com"

"""
    使用session第二次请求不需要重新带入新的请求头，会将之前的请求头数据自动载入
"""
response = session.get(url_2)
print("第二次请求的请求头数据为：", response.request.headers)
print("第二次请求的响应头数据为：", response.headers)